﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DeviceInfoManage.DataAccessLayer;

namespace DeviceInfoManage.BusinessLogicLayer
{
    /// <summary>
    /// 设备送修管理
    /// </summary>
    [Serializable]
    public class repairManage
    {

        #region 字段属性
        private int _repairid;
        private string _deviceuser;
        private string _departmentname;
        private DateTime _repairtime;
        private string _errtype;
        private string _devicemodel;
        private string _deviceserialnumber;
        private string _propertyserialnumber;
        private string _repairstation;
        private string _errdetail;
        /// <summary>
        /// 维修记录ID
        /// </summary>
        public int repairId
        {
            set { _repairid = value; }
            get { return _repairid; }
        }
        /// <summary>
        /// 使用人
        /// </summary>
        public string deviceUser
        {
            set { _deviceuser = value; }
            get { return _deviceuser; }
        }
        /// <summary>
        /// 所属部门
        /// </summary>
        public string departmentName
        {
            set { _departmentname = value; }
            get { return _departmentname; }
        }
        /// <summary>
        /// 送修时间
        /// </summary>
        public DateTime repairTime
        {
            set { _repairtime = value; }
            get { return _repairtime; }
        }
        /// <summary>
        /// 故障类型
        /// </summary>
        public string errType
        {
            set { _errtype = value; }
            get { return _errtype; }
        }
        /// <summary>
        /// 设备型号
        /// </summary>
        public string deviceModel
        {
            set { _devicemodel = value; }
            get { return _devicemodel; }
        }
        /// <summary>
        /// 设备编号
        /// </summary>
        public string deviceSerialNumber
        {
            set { _deviceserialnumber = value; }
            get { return _deviceserialnumber; }
        }
        /// <summary>
        /// 资产编号
        /// </summary>
        public string propertySerialNumber
        {
            set { _propertyserialnumber = value; }
            get { return _propertyserialnumber; }
        }
        /// <summary>
        /// 送修地点
        /// </summary>
        public string repairStation
        {
            set { _repairstation = value; }
            get { return _repairstation; }
        }
        /// <summary>
        /// 故障详细情况
        /// </summary>
        public string errDetail
        {
            set { _errdetail = value; }
            get { return _errdetail; }
        }
        #endregion 字段属性

        #region 构造函数
        public repairManage()
        { }
        #endregion

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("repairId", "repairManage");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int repairId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from repairManage");
            strSql.Append(" where repairId=@repairId ");
            SqlParameter[] parameters = {
					new SqlParameter("@repairId", SqlDbType.Int,4)};
            parameters[0].Value = repairId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(repairManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into repairManage(");
            strSql.Append("deviceUser,departmentName,repairTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,repairStation,errDetail)");
            strSql.Append(" values (");
            strSql.Append("@deviceUser,@departmentName,@repairTime,@errType,@deviceModel,@deviceSerialNumber,@propertySerialNumber,@repairStation,@errDetail)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@deviceUser", SqlDbType.VarChar,20),
					new SqlParameter("@departmentName", SqlDbType.VarChar,20),
					new SqlParameter("@repairTime", SqlDbType.DateTime),
					new SqlParameter("@errType", SqlDbType.NVarChar,20),
					new SqlParameter("@deviceModel", SqlDbType.VarChar,50),
					new SqlParameter("@deviceSerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@propertySerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@repairStation", SqlDbType.VarChar,50),
					new SqlParameter("@errDetail", SqlDbType.Text)};
            parameters[0].Value = model.deviceUser;
            parameters[1].Value = model.departmentName;
            parameters[2].Value = model.repairTime;
            parameters[3].Value = model.errType;
            parameters[4].Value = model.deviceModel;
            parameters[5].Value = model.deviceSerialNumber;
            parameters[6].Value = model.propertySerialNumber;
            parameters[7].Value = model.repairStation;
            parameters[8].Value = model.errDetail;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(repairManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update repairManage set ");
            strSql.Append("deviceUser=@deviceUser,");
            strSql.Append("departmentName=@departmentName,");
            strSql.Append("repairTime=@repairTime,");
            strSql.Append("errType=@errType,");
            strSql.Append("deviceModel=@deviceModel,");
            strSql.Append("deviceSerialNumber=@deviceSerialNumber,");
            strSql.Append("propertySerialNumber=@propertySerialNumber,");
            strSql.Append("repairStation=@repairStation,");
            strSql.Append("errDetail=@errDetail");
            strSql.Append(" where repairId=@repairId ");
            SqlParameter[] parameters = {
					new SqlParameter("@repairId", SqlDbType.Int,4),
					new SqlParameter("@deviceUser", SqlDbType.VarChar,20),
					new SqlParameter("@departmentName", SqlDbType.VarChar,20),
					new SqlParameter("@repairTime", SqlDbType.DateTime),
					new SqlParameter("@errType", SqlDbType.NVarChar,20),
					new SqlParameter("@deviceModel", SqlDbType.VarChar,50),
					new SqlParameter("@deviceSerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@propertySerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@repairStation", SqlDbType.VarChar,50),
					new SqlParameter("@errDetail", SqlDbType.Text)};
            parameters[0].Value = model.repairId;
            parameters[1].Value = model.deviceUser;
            parameters[2].Value = model.departmentName;
            parameters[3].Value = model.repairTime;
            parameters[4].Value = model.errType;
            parameters[5].Value = model.deviceModel;
            parameters[6].Value = model.deviceSerialNumber;
            parameters[7].Value = model.propertySerialNumber;
            parameters[8].Value = model.repairStation;
            parameters[9].Value = model.errDetail;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int repairId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from repairManage ");
            strSql.Append(" where repairId=@repairId ");
            SqlParameter[] parameters = {
					new SqlParameter("@repairId", SqlDbType.Int,4)};
            parameters[0].Value = repairId;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public repairManage GetModel(int repairId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 repairId,deviceUser,departmentName,repairTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,repairStation,errDetail from repairManage ");
            strSql.Append(" where repairId=@repairId ");
            SqlParameter[] parameters = {
					new SqlParameter("@repairId", SqlDbType.Int,4)};
            parameters[0].Value = repairId;

            repairManage model = new repairManage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["repairId"].ToString() != "")
                {
                    model.repairId = int.Parse(ds.Tables[0].Rows[0]["repairId"].ToString());
                }
                model.deviceUser = ds.Tables[0].Rows[0]["deviceUser"].ToString();
                model.departmentName = ds.Tables[0].Rows[0]["departmentName"].ToString();
                if (ds.Tables[0].Rows[0]["repairTime"].ToString() != "")
                {
                    model.repairTime = DateTime.Parse(ds.Tables[0].Rows[0]["repairTime"].ToString());
                }
                model.errType = ds.Tables[0].Rows[0]["errType"].ToString();
                model.deviceModel = ds.Tables[0].Rows[0]["deviceModel"].ToString();
                model.deviceSerialNumber = ds.Tables[0].Rows[0]["deviceSerialNumber"].ToString();
                model.propertySerialNumber = ds.Tables[0].Rows[0]["propertySerialNumber"].ToString();
                model.repairStation = ds.Tables[0].Rows[0]["repairStation"].ToString();
                model.errDetail = ds.Tables[0].Rows[0]["errDetail"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select repairId,deviceUser,departmentName,repairTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,repairStation,errDetail ");
            strSql.Append(" FROM repairManage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" repairId,deviceUser,departmentName,repairTime,errType,deviceModel,deviceSerialNumber,propertySerialNumber,repairStation,errDetail ");
            strSql.Append(" FROM repairManage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM errTypeInfo ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet QueryDevice(repairManage mr)
        {
            try
            {
                string queryString = "select * from deRepairInfoView";
                string strSql = string.Empty;
                if (mr.departmentName != "") strSql = " and departmentName like '%" + mr.departmentName + "%'";
                if (mr.errType != "") strSql += " and errTypeName like '%" + mr.errType + "%'";
                if (mr.deviceUser != "") strSql += " and deviceUser like '%" + mr.deviceUser + "%'";
                if (mr.propertySerialNumber != "") strSql += " and propertySerialNumber like '%" + mr.propertySerialNumber + "%'";
                if (mr.repairTime != DateTime.MinValue) strSql += " and repairTime = '" + mr.repairTime.ToShortDateString() + "'";
                if (strSql.StartsWith(" and"))
                {
                    strSql = strSql.Remove(0, 4);
                    queryString += " where" + strSql;
                }
                ///*如果用户设备了开始时间的查询条件*/
                //if (startTimeString != "")
                //    queryString += " and devicePurchaseTime>'" + Convert.ToDateTime(startTimeString) + "'";
                ///*如果用户设置了结束时间的查询条件*/
                //if (endTimeString != "")
                //    queryString += " and devicePurchaseTIme<'" + Convert.ToDateTime(endTimeString) + "'";

                DataBase db = new DataBase();
                return db.GetDataSet(queryString);
            }
            catch
            {
                return null;
            }
        }

        #endregion  成员方法
    }
}
